package model;

import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;

import com.androidplot.xy.XYPlot;

public class ZoomPlot extends XYPlot {
	private ScaleGestureDetector mScaleDetector;
	private float mScaleFactor = 1.f;

	public ZoomPlot(Context context, String title) {
		super(context, title);
		mScaleDetector = new ScaleGestureDetector(context, new ScaleListener());
	}

	public ZoomPlot(Context context, AttributeSet attributes) {
		super(context, attributes);
		mScaleDetector = new ScaleGestureDetector(context, new ScaleListener());
	}

	public ZoomPlot(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		mScaleDetector = new ScaleGestureDetector(context, new ScaleListener());
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		// Let the ScaleGestureDetector inspect all events.
		mScaleDetector.onTouchEvent(event);
		return true;
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);

		canvas.save();
		canvas.scale(mScaleFactor, mScaleFactor);
		// ...
		// Your onDraw() code
		// ...
		canvas.restore();
	}

	private class ScaleListener extends
			ScaleGestureDetector.SimpleOnScaleGestureListener {
		@Override
		public boolean onScale(ScaleGestureDetector detector) {
			mScaleFactor *= detector.getScaleFactor();

			// Don't let the object get too small or too large.
			mScaleFactor = Math.max(0.1f, Math.min(mScaleFactor, 5.0f));

			invalidate();
			return true;
		}
	}
}